/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import model.DiagramModel;
import data.PredefinedObjects;
import diagram.Graphical;
import diagram.Node;
import diagram.handles.ConnectHandle;
import diagram.handles.Handle;
import diagram.handles.InputHandle;
import diagram.handles.OutputHandle;
import diagram.handles.TriggerHandle;
import diagram.locations.AbsoluteLocation;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

/**
 *
 * @author Jelle
 */
public class AddAction extends AbstractAction {

    private DiagramModel model;
    private Node node;
    private Handle handle;
    private Handle baseHandle;
    private int modus;
    private PredefinedObjects objects;

    private void init() {
        objects = new PredefinedObjects();
        node = new Node();
        node = objects.getSimpleNode();

    }

    public AddAction(DiagramModel model) {
        init();
        this.model = model;
        modus = 0;
    }

    public AddAction(Node node, DiagramModel model) {
        init();
        this.node = node;
        this.model = model;
        modus = 1;
    }


//    public AddAction(AbsoluteLocation location) {
//        init();
//        System.out.println(""+location.getX()+"-"+location.getY());
//        node.setUpperLeft(location);
//        node.setLowerRight(new FlexibleRelativeLocation(node.getUpperLeft(), 100, 50));
//        System.out.println(""+location.getX()+"-"+location.getY());
//        modus = 1;
//    }
    public AddAction(String s, AbsoluteLocation location, DiagramModel model) {
        init();
        this.model = model;
        if (s.equals("normal")) {
            modus = 3;
            if (location != null) {
                node.setUpperLeft(location);
            }
            //node.print();
        } else if (s.equals("simple")) {
            modus = 3;
        } else if (s.equals("integer")) {
            modus = 4;
        } else if (s.equals("triggerhandle")) {

            Graphical gr = model.getSelected();
            Class toBedefined; // java reflection
            ClassLoader classLoader = this.getClass().getClassLoader();

            try {
                toBedefined = classLoader.loadClass(gr.getClass().getName());
                System.out.println("c:" + toBedefined.getName());

                if (toBedefined.getName().equals("diagram.Node")) {
                    System.out.println("add triggerhandle / node");
                    node = (Node) model.getSelected();
                    handle = new TriggerHandle(node,5);
                    node.setLevel(0);
                    handle.setLevel(10);
//                    System.out.println("klaske" + node.getClass() + "ssss" + node.getName());
//                    Location loc = new RectangleLineLocation(
//                            new FlexibleRelativeLocation(node.getUpperLeft(), 0, 0), node.getUpperLeft(), node.getLowerRight());
//                    handle.setLocation(loc);
                    handle.attach(node);
                } else if (toBedefined.getName().equals("diagram.handles.InputHandle")) {
                    System.out.println("add triggerhandle / inputhandle");

                   // baseHandle = (InputHandle)toBedefined.cast(baseHandle);
                   // if (baseHandle != null) {
                        //System.out.println("klaske" + baseHandle.getClass() + "ssss");
                         baseHandle = (Handle) model.getSelection("handle").get(0);
                         handle = new TriggerHandle(baseHandle,5);
                         handle.setLevel(10);
//                        Location loc = new RectangleLineLocation(
//                                new FlexibleRelativeLocation(
//                                new AbsoluteLocation(
//                                baseHandle.getLocation().getX() - objects.getInputHandleX(),
//                                baseHandle.getLocation().getY() + objects.getInputHandleY() + 5),
//                                0, 0),
//                                baseHandle.getLocation(),
//                                new AbsoluteLocation(
//                                baseHandle.getLocation().getX() + objects.getInputHandleX(),
//                                baseHandle.getLocation().getY() + objects.getInputHandleY()));
                        //handle.setLocation(loc);
                        handle.attach(baseHandle);
                    //}

                }
            } catch (ClassNotFoundException ex) {
            }

            modus = 5;
        } else if (s.equals("inputhandle")) {
            node = (Node) model.getSelected();
            //VerticalLineLocation loc = new VerticalLineLocation(new FlexibleRelativeLocation(node.getUpperLeft(), 0, 0),node.getUpperLeft(),new AbsoluteLocation(node.getUpperLeft().getX(), node.getLowerRight().getY()));
            // Location loc = new AbsoluteLocation(node.getUpperLeft().getX(), node.getUpperLeft().getY());
            // h.setLocation(loc);
            InputHandle h = new InputHandle(node, 30);
            h.setLevel(5);
            h.attach(node);
            handle = h;
            modus = 5;
        } else if (s.equals("outputhandle")) {
            System.out.println("outputhandle maken");
            node = (Node) model.getSelected();
            OutputHandle h = new OutputHandle(node, 30);
            h.setLevel(5);
            h.attach(node);
            handle = h;
            modus = 5;
        } else if (s.equals("connect")) {
            System.out.println("addconnect");
            node = (Node) model.getSelected();
            ConnectHandle h = new ConnectHandle(node);
            h.setLevel(15);
            handle = h;
            modus = 5;
        }
    }

    public AddAction(Node node, Handle handle, DiagramModel model) {
        init();
        this.node = node;
        this.handle = handle;
        this.model = model;
        modus = 2;
    }

    public void actionPerformed(ActionEvent e) {
        switch (modus) {
            case 0:
                System.out.println("AddAction mode 0");
                break;
            case 1:
                System.out.println("AddAction mode 1");
                model.addObject(node);
                break;
            case 2:
                System.out.println("AddAction mode 2");
                model.addObject(node);
                model.addObject(handle);
                // model.setLastSelected(node);
                break;
            case 3:
                System.out.println("AddAction mode 3");
                model.addObject(node);
                model.addObject(objects.getResizeHandle(node));
                break;
            case 4:
                System.out.println("AddAction mode 4");
                break;
            case 5:
                System.out.println("AddAction mode 5");
                model.addObject(handle);
                break;
        }
    }
}
